<template>
  <a-layout class="basic-layout">
    <!-- 侧边栏 -->
    <a-layout-sider
      v-model:collapsed="collapsed"
      :trigger="null"
      collapsible
      theme="dark"
      width="256"
    >
      <div class="logo">
        <span v-if="!collapsed" class="logo-text">LifeVerse</span>
      </div>
      
      <a-menu
        v-model:selectedKeys="selectedKeys"
        mode="inline"
        theme="dark"
        @click="handleMenuClick"
      >
        <a-menu-item key="/dashboard">
          <dashboard-outlined />
          <span>仪表盘</span>
        </a-menu-item>
        <a-menu-item key="/life-entities">
          <node-index-outlined />
          <span>生命体管理</span>
        </a-menu-item>
        <a-menu-item key="/network">
          <share-alt-outlined />
          <span>网络关系</span>
        </a-menu-item>
        <a-menu-item key="/monitoring">
          <monitor-outlined />
          <span>实时监控</span>
        </a-menu-item>
      </a-menu>
    </a-layout-sider>

    <!-- 主内容区 -->
    <a-layout>
      <!-- 顶部导航 -->
      <a-layout-header class="layout-header">
        <div class="header-left">
          <menu-unfold-outlined
            v-if="collapsed"
            class="trigger"
            @click="() => (collapsed = !collapsed)"
          />
          <menu-fold-outlined
            v-else
            class="trigger"
            @click="() => (collapsed = !collapsed)"
          />
        </div>

        <div class="header-right">
          <a-space>
            <!-- 主题切换 -->
            <a-tooltip :title="themeStore.isDark ? '切换到亮色主题' : '切换到暗色主题'">
              <a-button 
                type="text" 
                :icon="themeStore.isDark ? h(SunOutlined) : h(MoonOutlined)"
                @click="themeStore.toggleTheme"
                class="theme-toggle"
              />
            </a-tooltip>
            
            <!-- 通知 -->
            <a-badge :count="5">
              <bell-outlined class="header-icon" />
            </a-badge>
            
            <!-- 用户菜单 -->
            <a-dropdown>
              <a-avatar class="user-avatar">
                <template #icon><user-outlined /></template>
              </a-avatar>
              <template #overlay>
                <a-menu>
                  <a-menu-item key="profile">
                    <user-outlined />
                    个人资料
                  </a-menu-item>
                  <a-menu-item key="settings">
                    <setting-outlined />
                    设置
                  </a-menu-item>
                  <a-menu-divider />
                  <a-menu-item key="logout">
                    <logout-outlined />
                    退出登录
                  </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
          </a-space>
        </div>
      </a-layout-header>

      <!-- 内容区域 -->
      <a-layout-content class="layout-content">
        <router-view />
      </a-layout-content>

      <!-- 底部 -->
      <a-layout-footer class="layout-footer">
        LifeVerse ©2024 Created by AI Intelligence
      </a-layout-footer>
    </a-layout>
  </a-layout>
</template>

<script setup lang="ts">
import { ref, watch, h } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useThemeStore } from '@/stores/theme'
import {
  MenuFoldOutlined,
  MenuUnfoldOutlined,
  UserOutlined,
  BellOutlined,
  SettingOutlined,
  LogoutOutlined,
  DashboardOutlined,
  NodeIndexOutlined,
  ShareAltOutlined,
  MonitorOutlined,
  SunOutlined,
  MoonOutlined
} from '@ant-design/icons-vue'

const route = useRoute()
const router = useRouter()
const themeStore = useThemeStore()

// 响应式数据
const collapsed = ref(false)
const selectedKeys = ref<string[]>([])

// 菜单点击处理
const handleMenuClick = ({ key }: { key: string }) => {
  router.push(key)
}

// 监听路由变化更新选中菜单
watch(
  () => route.path,
  (newPath) => {
    selectedKeys.value = [newPath]
  },
  { immediate: true }
)
</script>

<style scoped>
.basic-layout {
  min-height: 100vh;
}

.logo {
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 16px;
  border-bottom: 1px solid #f0f0f0;
}

.logo-text {
  margin-left: 12px;
  font-size: 18px;
  font-weight: bold;
  color: white;
}

.layout-header {
  background: #fff;
  padding: 0 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
}

.header-left {
  display: flex;
  align-items: center;
}

.trigger {
  font-size: 18px;
  line-height: 64px;
  padding: 0 24px;
  cursor: pointer;
  transition: color 0.3s;
}

.trigger:hover {
  color: #1890ff;
}

.header-right {
  display: flex;
  align-items: center;
}

.header-icon {
  font-size: 16px;
  cursor: pointer;
  padding: 8px;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.header-icon:hover {
  background-color: #f5f5f5;
}

.user-avatar {
  cursor: pointer;
}

.theme-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: 4px;
  transition: all 0.3s;
}

.theme-toggle:hover {
  background-color: rgba(0, 0, 0, 0.04);
}

.layout-content {
  margin: 24px;
  padding: 24px;
  background: #fff;
  border-radius: 8px;
  min-height: calc(100vh - 200px);
}

.layout-footer {
  text-align: center;
  background: #f0f2f5;
}
</style>